package com.spdg.ring.bo;

import android.app.Activity;
import android.view.View;
import android.widget.Toast;
import cn.wolf.base.BaseBo;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.tools.CommonUtils;

import com.spdg.ring.R;
import com.spdg.ring.resp.ArticleResp;
import com.spdg.ring.sqlite.model.Article;
import com.spdg.ring.sqlite.service.ArticleService;
import com.spdg.ring.task.ArticleTask;

/**
 * <p>Title: 详细内容逻辑类      </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-30             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleBo extends BaseBo implements HttpCallBack<ArticleResp> {
	private String url;
	private HttpCallBack<ArticleResp> icall;
	private String key;
	private Article article;
	View loadingBar;

	private View convertView;

	public ArticleBo(Activity activity, HttpCallBack<ArticleResp> icall, String key) {
		super(activity);
		this.icall = icall;
		this.mActivity = activity;
		this.key = key;
	}

	public void setParams(String url) {
		this.url = url;
	}

	public void request() {
		request(null);
	}

	public void request(View view) {
		this.convertView = view;
		if (convertView != null) {
			loadingBar = convertView.findViewById(R.id.loading_bar);
			showLoadingBar();
		}
		// 读取缓存
		try {
			ArticleService articleService = ArticleService.newInstance(mActivity);
			article = articleService.findByKey(key);
		} catch (Exception e) {
		}

		if (!CommonUtils.isNetConnectionAvailable(mActivity)) {
			if (article != null) {
				ArticleResp resp = new ArticleResp();
				resp.setStatus(BaseResp.SUCCESS);
				resp.setConvertView(convertView);
				resp.setArticle(article);

				call(resp);
			}
		} else {
			if (article != null && Article.IS_ONLINE_ARTICLE.equals(article.getIsOnline())) {

				ArticleResp resp = new ArticleResp();
				resp.setStatus(BaseResp.SUCCESS);
				resp.setConvertView(convertView);
				resp.setArticle(article);

				call(resp);
			} else {
				// 请求网络数据
				ArticleTask task = new ArticleTask(this, mActivity, url, key);
				task.execute();
			}
		}

	}

	public void hideLoadingBar() {
		if (loadingBar != null) {
			loadingBar.setVisibility(View.GONE);
		}
	}

	public void showLoadingBar() {
		if (loadingBar != null) {
			loadingBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void call(ArticleResp response) {
		hideLoadingBar();

		article = response.getArticle();
		response.setConvertView(convertView);
		// 回调数据
		icall.call(response);
	}

	@Override
	public void progress(Object... obj) {
	}

	@Override
	public void onNetWorkError() {
		Toast.makeText(mActivity, mActivity.getString(R.string.network_error), Toast.LENGTH_SHORT)
				.show();
	}

}
